#       FrontendFeedEntry_test.py
#       
#       Copyright 2008 Joshua Scotton <josh@joshuascotton.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2008, Joshua Scotton"
__author__ = "Joshua Scotton <josh@joshuascotton.com>"

import unittest
import sys
import os

from pysqlite2 import dbapi2 as sqlite

sys.path += [os.path.abspath('..')]

from frontend.medialibrary.feeds import Feed, Entry

class FrontendFeedEntryTest(unittest.TestCase):
    
    entry_title = "Test Entry"
    entry_desc = "This is a test description"
    entry_id = "1"
    entry_time = "13:02"
    entry_date = "02/02/08"

    def setUp(self):
        """Sets up Entry object for the test"""
        self.feed_db = os.path.abspath('.' + '/data/' + self.__str__() + '.db')
        if os.path.exists(self.feed_db):
            os.remove(self.feed_db) # Start fresh to avoid table creation error
        db_conn = sqlite.connect(self.feed_db)
        db_cursor = db_conn.cursor()
        db_cursor.execute("""CREATE TABLE feed(
                                url TEXT, 
                                title TEXT, 
                                subtitle TEXT, 
                                description TEXT, 
                                time TIME,
                                date DATE,
                                PRIMARY KEY(url))""")
        db_cursor.execute("""CREATE TABLE entry(
                                feed_url TEXT,
                                title TEXT,
                                description TEXT,
                                isread TEXT,
                                time TIME,
                                date DATE,
                                id TEXT,
                                PRIMARY KEY(id))""")
        db_conn.commit()
        entry_row = ("url2",
                     "entry1", 
                     "desc",
                     "NO",
                     "1",
                     "2008-01-01",
                     "12:00:00")
        db_cursor.execute("""INSERT INTO entry(feed_url,title,description,isread,id,date,time) 
                                 VALUES (?,?,?,?,?,?,?)""", entry_row)
        db_conn.commit()
        feed_row = ("url2", 
                    "title2", 
                    "subtitle2", 
                    "description2",
                    "2008-01-01",
                    "12:00:00")
        db_cursor.execute("""INSERT INTO feed(url,title,subtitle,description,date,time) 
                             VALUES (?,?,?,?,?,?)""", feed_row)
        db_conn.commit()
        db_conn.close()
        self.entry = Entry(self.entry_title, self.entry_desc, self.entry_id, self.entry_time, self.entry_date, False, self.feed_db)

    def tearDown(self):
        """
        Removes test database file
        """
        if os.path.exists(self.feed_db):
                os.remove(self.feed_db)

    def testInitReadEntry(self):
        """
        testInitReadEntry: Tests if Entry can handle the additional read parameter
        """
        new_entry = Entry( self.entry_title, self.entry_desc, self.entry_id,
                           self.entry_time, self.entry_date, True)
        self.assertTrue(new_entry.is_read())
                           

    def testGetTitle(self):
        """
        testGetTitle: Tests if the title parameter is set properly
        """
        self.assertEqual(self.entry_title, self.entry.get_title())
        
    def testGetDescription(self):
        """
        testGetDescription: Tests if the description parameter is set properly
        """
        self.assertEqual(self.entry_desc, self.entry.get_description())
        
    def testGetId(self):
        """
        testGetId: Tests if the id parameter is set properly
        """
        self.assertEqual(self.entry_id, self.entry.get_id())

    def testGetTime(self):
        """
        testGetTime: Tests if the time parameter is set properly
        """
        self.assertEqual(self.entry_time, self.entry.get_time())
        
    def testGetDate(self):
        """
        testGetDate: Tests if the date parameter is set properly
        """
        self.assertEqual(self.entry_date, self.entry.get_date())
        
    def testIsRead(self):
        """
        testIsRead: Tests if the read parameter of Entry defaults to False
        """
        self.assertFalse(self.entry.is_read())
    
    def testMarkAsRead(self):
        """
        testMarkAsRead: Tests if mark_as_read() changes the read parameter
        """
        self.entry.mark_as_read()
        self.assertTrue(self.entry.is_read())
    
    def testMarkAsUnread(self):
        """
        testMarkAsUnread: Tests if mark_as_unread() changes the read parameter
        """
        self.entry.mark_as_read()
        self.entry.mark_as_unread()
        self.assertFalse(self.entry.is_read())
    
    def testSetRead(self):
        """
        testSetRead: Tests if _set_read() changes the read parameter
        """
        self.entry._set_read(True)
        self.assertTrue(self.entry.is_read())
        
    def testDatabaseConnectionSetRead(self):
        """
        testDatabaseConnectionSetRead: Tests if _set_read changes database properly
        """
        feed = Feed("url2",self.feed_db)
        self.entry._set_read(True)
        self.assertEqual(feed.get_number_of_unread(),0)
        
        
